﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;

namespace winpackman
{
    public partial class FRMsettings : Form
    {
        public winpackman_class main_class = new winpackman_class();

        public FRMsettings()
        {
            InitializeComponent();
        }

        private void FRMsettings_Load(object sender, EventArgs e)
        {

            foreach (CultureInfo regio in CultureInfo.GetCultures(CultureTypes.NeutralCultures))
            {
                ListViewItem lvi = new ListViewItem(regio.EnglishName);
                if (main_class.config.Language.Contains(regio.TwoLetterISOLanguageName))
                    lvi.Checked = true;
                listView1.Items.Add(lvi);
            }

            //load config
            CBremovecache.Checked = main_class.config.clearcache;
            CBhidex86.Checked = main_class.config.hidex86;
            CBclearquery.Checked = main_class.config.clearqueryinstall;
            CBbeta.Checked = main_class.config.viewbeta;
        }

        private void BTsaveconfig_Click(object sender, EventArgs e)
        {
            String[] language = {"en"};
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
            foreach (ListViewItem item in listView1.CheckedItems)
            {
                string regio = cultures.First(c => c.EnglishName == item.Text).TwoLetterISOLanguageName;
                if (!language.Contains(regio))
                {
                    Array.Resize(ref language, language.Length + 1);
                    language[language.Length - 1] = regio;
                }
            }
            Array.Resize(ref main_class.config.Language, language.Length);
            main_class.config.Language = language;

            main_class.config.hidex86 = CBhidex86.Checked;
            main_class.config.clearcache = CBremovecache.Checked;
            main_class.config.clearqueryinstall = CBclearquery.Checked;
            main_class.config.viewbeta = CBbeta.Checked;
            main_class.writeconfig();
            Close();
        }

        private void BTclearcache_Click(object sender, EventArgs e)
        {
            String[] files = { "Cache\\category.list", "Cache\\packages.list", "Cache\\config.xml" };
            foreach (String lang in main_class.config.Language)
            {
                Array.Resize(ref files, files.Length + 1);
                files[files.Length - 1] = "Cache\\pack_" + lang + ".list";
            }
            if (!CBremovecache.Checked)
            {
                Dictionary<string, InstallLanguage> lang = new Dictionary<string,InstallLanguage>();
                foreach (Software appA in main_class.Softwarelist.Values)
                {
                    bool loop = false;
                doloop:
                    if (loop) lang = appA.x86.Language;
                    else lang = appA.x64.Language;
                    loop = !loop;
                    foreach (InstallLanguage appB in lang.Values)
                    {
                        if ((appB.MD5 != "") && (File.Exists("Cache/" + appB.Filename)))
                        {
                            string MD5 = main_class.GetMD5HashFromFile("Cache/" + appB.Filename);
                            if (String.Compare(MD5, appB.MD5, true) == 0)
                            {
                                Array.Resize(ref files, files.Length + 1);
                                files[files.Length - 1] = "Cache\\" + appB.Filename;
                            }
                            if (main_class.debug.md5out)
                            {
                                StreamWriter sw = new StreamWriter("MD5.txt", true);
                                sw.Write(String.Compare(MD5, appB.MD5, true));
                                sw.WriteLine(";" + appB.Filename + ";" + MD5);
                                sw.Close();
                            }
                        }
                    }
                    if (loop) goto doloop;
                }
            }
            int count = 0;
            if (!main_class.debug.md5out)
            {
                foreach (String file in Directory.GetFiles("Cache"))
                {
                    if (!files.Contains(file))
                    {
                        File.Delete(file);
                        count++;
                    }
                }
            }
            MessageBox.Show(count.ToString() + " items removed", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
